<?php

class Player {
	
	public $id;
	public $ime;
	public $prezime;
	public $slika;
	public $profilnaSlika;
	public $dres;
	public $datumRodjenja;
	public $visina;
	public $tezina;
	public $ruka;
	
	public $pozicije;
	public $stats;
	public $totalStats;	
	
	public $achievements;
	public $totalAchievements;
	
	// constructor
	
	public function __construct($i = 0) {
		$this->id = $i;
		if ($i != 0) {
			$n = $this->getfromdb($i);
			$this->ime = $n['ime'];
			$this->prezime = $n['prezime'];
			$this->slika = $n['slika'];
			$this->profilnaSlika = $n['profilnaSlika'];
			$this->dres = $n['dres'];
			$this->datumRodjenja = date("d.m.Y.", strtotime($n['datumRodjenja']));
			$this->visina = intval($n['visina']);
			$this->tezina = intval($n['tezina']);
			$this->ruka = $n['ruka'];
	
			$this->getPozicije();
		}
		// print "Pozvan je konstuktor za objekat Player!";
	}
	
	// functions
	
	private function getfromdb($number) {
		$q = "SELECT id, ime, prezime, slika, profilnaSlika, dres, datumRodjenja, visina, tezina, ruka FROM igrac WHERE id = " . $number;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (!mysql_num_rows($r)) throw new Exception("Ne postoji igrac za zadati ID!");
		return (mysql_fetch_array($r));
	}
	
	private function getPozicije() {
		$q = "SELECT pozicija FROM pozicije WHERE igrac = " . $this->id;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r)) {
			while ($n = mysql_fetch_array($r)) {
				$this->pozicije[] = $n['pozicija'];
			}
		}
	}
	
	public function getTeamInfo() {
		$q = "SELECT tim FROM igra_za WHERE igrac = " . $this->id;
		$r = mysql_query($q);
		$n = mysql_fetch_array($r);
		
		$tim = new Tim($n['tim']);
		return $tim;
	}
	
	public function getStats($match) {
		$this->stats[$match] = new Statistics($this->id, $match);
	}
	
	public function getAllStats() {
		$q = "SELECT mec FROM statistika WHERE igrac = " . $this->id;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r)) {
			while ($n = mysql_fetch_array($r)) {
				$this->stats[] = new Statistics($this->id, $n['mec']);
			}
		}
	}
	
	public function calculateTotalStats() {
		$this->totalStats = new Statistics(0, 0);
		if (count ($this->stats)) {
			foreach($this->stats as $stat) {
				$this->totalStats->licne += $stat->licne;
				$this->totalStats->ft += $stat->ft;
				$this->totalStats->fg += $stat->fg;
				$this->totalStats->threept += $stat->threept;
				$this->totalStats->mft += $stat->mft;
				$this->totalStats->mfg += $stat->mfg;
				$this->totalStats->mtp += $stat->mtp;
				$this->totalStats->drbd += $stat->drbd;
				$this->totalStats->orbd += $stat->orbd;
				$this->totalStats->blk += $stat->blk;
				$this->totalStats->ast += $stat->ast;
				$this->totalStats->stl += $stat->stl;
	
				$this->totalStats->pts += $stat->pts;
				$this->totalStats->totalRbd += $stat->totalRbd;
				$this->totalStats->BLindex += $stat->BLindex;
			}
		}
	}
	
	public function getAchievements() {
		$q = "SELECT mec FROM statistika WHERE igrac = " . $this->id;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r)) {
			while ($n = mysql_fetch_array($r)) {
				$this->achievements[] = new Achievement($this->id, $n['mec']);
			}
		}
	}
	
	public function calculateTotalAchievements() {
		$this->totalAchievements = new Achievement(0, 0);
		
		if (count($this->achievements)) {
			foreach ($this->achievements as $ach) {
				$this->totalAchievements->topscorer += $ach->topscorer;
				$this->totalAchievements->doubledouble += $ach->doubledouble;
				$this->totalAchievements->trippledouble += $ach->trippledouble;
				$this->totalAchievements->quadrupledouble += $ach->quadrupledouble;
				$this->totalAchievements->gamemvp += $ach->gamemvp;
			}
		}
		
		$rounds = getAllRounds();
		foreach($rounds as $kolo) {
			$kolo->getMecevi();
			foreach($kolo->mecevi as $mec) {
				$mec->domacin->getRoster();
				$mec->gost->getRoster();
				foreach($mec->domacin->roster as $igrac) {
					$igrac->getStats($mec->id);
				}
				foreach($mec->gost->roster as $igrac) {
					$igrac->getStats($mec->id);
				}
				$mec->domacin->stats = $mec->domacin->getMatchStats($mec->id);
				$mec->gost->stats = $mec->gost->getMatchStats($mec->id);
			}
			if ($kolo->findMVP()->id == $this->id) {
			$this->totalAchievements->roundmvp++;
			}
		}
	}

	// admin
	
	public function insertIntoDb() {
		$q = "INSERT INTO igrac (ime, prezime, dres, datumRodjenja, visina, tezina, ruka, slika, profilnaSlika) VALUES ('".$this->ime."', '".$this->prezime."', '".$this->dres."', '".$this->datumRodjenja."', '".$this->visina."', '".$this->tezina."', '".$this->ruka."', '".$this->slika."', '".$this->profilnaSlika."')";
echo $q;		$r = mysql_query("set names 'utf8'");
		if (mysql_query($q)) {
			echo "Novi igrac je dodat u bazu podataka";
		}
		
		$this->id = mysql_insert_id();
		
		foreach($this->pozicije as $poz) {
			$qp = "INSERT INTO pozicije (igrac, pozicija) VALUES ('".$this->id."','".$poz."')";
			echo $qp;
			mysql_query($qp) or die ("Nisam dodao poziciju $poz!");
			echo "Dodata pozicija ".$poz."<br />";
		}
	}

} // end of class Player


?>